<?php
/* 
 * Copyright (c) 2018-2021   All rights reserved.
 * 
 * 创建时间：2021-09-02 18:52
 *
 * 项目：rm  -  $  - run.php
 *
 * 作者：liwei 
 */


echo \modules\levpays\modules\alipay\widgets\pay\payWidget::miniJs();
?>

<script>
(function () {
    'use strict';

jQuery(function () {

<?php if (Lev::ckmobile() || Lev::checkAlipayUserAgent()):?>

    <?php if (\modules\levpays\modules\alipay\alipayHelper::checkAliMiniProgramUserAgent()): ?>

    var buyer_id = '', buyer_appid = '';
    levpaysPay.setPaytypeidInit[1] = function () {
        levpaysPay.pre = 'MINI';

        if (buyer_id && buyer_appid) return;
        window.setTimeout(function () {
            // 网页向小程序 postMessage 消息
            my.postMessage({buyer_id: 1});
            // 接收来自小程序的消息。
            my.onMessage = function (e) {
                e && e.buyer_id
                && (buyer_id = e.buyer_id)
                && (buyer_appid = e.buyer_appid);
                //myApp.alert(buyer_appid);
            }
        }, 101);
    };

    levpaysPay.levpaysAlipaySubmit = function () {
        levpaysPay.pre = 'MINI';

        var url = '<?=Lev::toReRoute(['pay/mini', 'id' => 'alipay', 'tradeNo' => $goods['tradeNo']])?>';
        Levme.ajaxv.getv(url, function (data, status) {
            //myApp.alert(buyer_appid+'==='+JSON.stringify(data), 10000);
            status >0 &&
            my.tradePay({
                // 调用统一收单交易创建接口（alipay.trade.create），获得返回字段支付宝交易号trade_no
                tradeNO: data.trade_no,
                success: (res) => {
                    // my.alert({
                    //     content: JSON.stringify(res),
                    // });
                    // levtoast('1111'+JSON.stringify(res));
                },
                fail: (res) => {
                    my.alert({
                        content: JSON.stringify(res),
                    });
                }
            });
        }, {buyer_id:buyer_id,appid:buyer_appid});

    };
    navigator.userAgent.indexOf('AliApp') > -1 && jQuery('.paytypeidbox-0').hide();
    //jQuery('.paytypeidbox-1 input[name=paytypeid]').click();

    <?php else:?>

    levpaysPay.setPaytypeidInit[1] = function () {
        levpaysPay.pre = 'WAP';

        var url = '<?=Lev::toReRoute(['pay/wap', 'id' => 'alipay', 'tradeNo' => $goods['tradeNo']])?>';
        //jQuery('.levpaysPaySubmitBtn').html('<a class="flex-box ju-sa color-white" href="'+url+'" target="_blank" _bk=1>前往支付宝支付</a>');
    };

    levpaysPay.levpaysAlipaySubmit = function () {
        levpaysPay.pre = 'WAP';

        var url = '<?=Lev::toReRoute(['pay/wap', 'id' => 'alipay', 'tradeNo' => $goods['tradeNo']])?>';
        var referer = '<?=Lev::$app['referer']?>';
        //window.location = url;
        var inApp = Levme.APP.ckAPP();
        if (inApp) {
            inApp.openToThirdPay(url, referer);
        }else {
            Levme.popupIframe.popupShow(0, url);
        }
    };

    <?php endif;?>

<?php else:?>

    levpaysPay.setPaytypeidInit[1] = function () {
        levpaysPay.pre = 'PC';

        jQuery('#levpays-qrcode').html('').removeClass('hiddenx');
        jQuery('.levpays-qrcode-box').addClass('hiddenx');
        jQuery('.levpaysPaySubmitBtn').html('<a class="flex-box ju-sa color-white" href="<?=Lev::toReRoute(['pay/pc', 'id' => 'alipay', 'tradeNo'=>$goods['tradeNo']])?>" target="_blank" _bk=1>前往支付</a>');
    };

    levpaysPay.levpaysAlipaySubmit = function () {
        levpaysPay.pre = 'PC';
    };

<?php endif;?>

});

})();
</script>
